﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace IdeaNMR.Web.Core
{
    public class UserDataManager : IDisposable
    {
        public static bool Exists(string username)
        {
            UserDataManager udm;
            udm = new UserDataManager(username);
            return true;
        }
        public static UserDataManager GetForUsername(string username)
        {
            UserDataManager result = new UserDataManager(username);
            return result;
        }

        private UserDataManager(string username)
        {
            // ALISTAIR modified 23/08/2011
            // Commented out if statement that throws exception,
            // since this exception is not handled anywhere
            //if (string.IsNullOrEmpty(username))
            //{
            //    throw new ArgumentException("Username must not be null or empty string.");
            //}
            // Instead, only attempt database connection if a username is available
            if (!(string.IsNullOrEmpty(username)))
            {
                Inmr = new IdeaNMRdbContainer();
                this.User = this.GetOrCreateUser(username);
            }
        }

        public void Modify(string firstname, string middlename, string lastname)
        {
            if (this.User.EntityState == System.Data.EntityState.Detached) Inmr.Attach(this.User);
            this.User.Firstname = firstname;
            this.User.Middlename = middlename;
            this.User.Lastname = lastname;
            Inmr.SaveChanges();
        }

        public User GetUser()
        {
            return this.User;
        }

        public User GetDetachedUser()
        {
            User result = GetOrCreateUser(this.Username);
            Inmr.Detach(result);
            return result;
        }

        /// <summary>
        /// A Username of Current User
        /// </summary>
        public string Username
        {
            get { return this.User.Username; }
            private set
            {
                this.User = GetOrCreateUser(value);
            }
        }

        private User User { get; set; }

        private IdeaNMRdbContainer Inmr { get; set; }

        private User GetOrCreateUser(string username)
        {
            User result;
            result = (from u in Inmr.Users
                      where u.Username == username
                      select u).FirstOrDefault();
            if (result == null)
            {
                result = new User();
                result.Username = username;
                result.Datetime = DateTime.Now;
                result.Firstname = result.Middlename = result.Lastname = string.Empty;
                Inmr.AddToUsers(result);
                Inmr.SaveChanges();
            }
            return result;
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.Inmr.Dispose();
        }

        #endregion
    }
}
